package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.response.MaterialInventoryList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MaterialInventoryMapper;
import com.ruoyi.system.domain.MaterialInventory;
import com.ruoyi.system.service.IMaterialInventoryService;
import com.ruoyi.common.core.text.Convert;

/**
 * 物料库存Service业务层处理
 *
 * @author tanshen
 * @date 2023-08-21
 */
@Service
public class MaterialInventoryServiceImpl extends ServiceImpl<MaterialInventoryMapper, MaterialInventory> implements IMaterialInventoryService {
    @Autowired
    private MaterialInventoryMapper materialInventoryMapper;

    /**
     * 查询物料库存
     *
     * @param id 物料库存主键
     * @return 物料库存
     */
    @Override
    public MaterialInventory selectMaterialInventoryById(Long id) {
        return materialInventoryMapper.selectMaterialInventoryById(id);
    }

    /**
     * 查询物料库存列表
     *
     * @param materialInventory 物料库存
     * @return 物料库存
     */
    @Override
    public List<MaterialInventoryList> selectMaterialInventoryList(MaterialInventory materialInventory) {
        return materialInventoryMapper.selectMaterialInventoryList(materialInventory);
    }

    /**
     * 新增物料库存
     *
     * @param materialInventory 物料库存
     * @return 结果
     */
    @Override
    public int insertMaterialInventory(MaterialInventory materialInventory) {
        return materialInventoryMapper.insertMaterialInventory(materialInventory);
    }

    /**
     * 修改物料库存
     *
     * @param materialInventory 物料库存
     * @return 结果
     */
    @Override
    public int updateMaterialInventory(MaterialInventory materialInventory) {
        return materialInventoryMapper.updateMaterialInventory(materialInventory);
    }

    /**
     * 批量删除物料库存
     *
     * @param ids 需要删除的物料库存主键
     * @return 结果
     */
    @Override
    public int deleteMaterialInventoryByIds(String ids) {
        return materialInventoryMapper.deleteMaterialInventoryByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除物料库存信息
     *
     * @param id 物料库存主键
     * @return 结果
     */
    @Override
    public int deleteMaterialInventoryById(Long id) {
        return materialInventoryMapper.deleteMaterialInventoryById(id);
    }

    @Override
    public List<MaterialInventory> selectByMaterialId(String materialId, Long supplierId) {
        QueryWrapper<MaterialInventory> wrapper = new QueryWrapper<>();
        wrapper.eq("material_id", materialId);
        wrapper.gt("entry_num", 0);
        if (Objects.nonNull(supplierId) && supplierId > 0) {
            wrapper.gt("supplier_id", supplierId);
        }
        wrapper.orderByDesc("entry_time");
        return materialInventoryMapper.selectList(wrapper);
    }
}
